package com.stone.business.gzrwd.action;

/**
 * <pre>
 * 变电运行工区工作任务单 
 * @author
 * </pre>
 */

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.workcard.dao.WorkcardDAO;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.util.SpringUtil;
import frm.ssh.util.page.Page;

public class DlqtzStaticsListAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private ZbjlCommonService myService;
	private Page pageInfo1;
	String month;
	String currentmonth;
    String ym;
    String monthid;
    //班组
	private List<TOrgan> bzList;
		public List<TOrgan> getBzList() {
		return bzList;
	}
	// 设备类型
	private List<TDictionary> sblxList;
		
	public void setBzList(List<TOrgan> bzList) {
		this.bzList = bzList;
	}

		public String getMonth() {
			return month;
		}

		public void setMonth(String month) {
			this.month = month;
		}

		public String getCurrentmonth() {
			return currentmonth;
		}

		public void setCurrentmonth(String currentmonth) {
			this.currentmonth = currentmonth;
		}

		public String getMonthid() {
			return monthid;
		}

		public void setMonthid(String monthid) {
			this.monthid = monthid;
		}
	// 无票列表
	private List<TGzrwd> listWp = null;
	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public DlqtzStaticsListAction() {
        this.entityClass = TGzrwd.class;
    }
private Long planid;
	public Long getPlanid() {
	return planid;
}

public void setPlanid(Long planid) {
	this.planid = planid;
}

/**
 * 设置查询条件
 */
public HashMap<String, Object> setSearchCondition() {
	
	HashMap<String, Object> m = new HashMap<String, Object>();
	List<Object> valsList = new ArrayList<Object>();
	StringBuffer sql = new StringBuffer();
	sql.append(" select A.cid as cid,");
	sql.append("  A.DEVICECATEGORY_CID as deviceCategoryCid,");
	sql.append("  A.DEPARTMENT_CID as departmentCid,");
	sql.append("  A.DEVICE_ID as deviceId,");
	sql.append("  A.DEVICE_NAME as deviceName,");
	sql.append("  A.ALLOWABLE_COUNT as allowableCount,");
	sql.append("  A.ACCUMULATED_COUNT as accumulatedCount,");
	sql.append("  A.ALLOWABLE_COUNT_A as allowableCountA,");
	sql.append("  A.ALLOWABLE_COUNT_B as allowableCountB,");
	sql.append("  A.ALLOWABLE_COUNT_C as allowableCountC,");
	sql.append("  A.ACCUMULATED_COUNT_A as accumulatedCountA,");
	sql.append("  A.ACCUMULATED_COUNT_B as accumulatedCountB,");
	sql.append("  A.ACCUMULATED_COUNT_C as accumulatedCountC,");
	sql.append("  A.ACCUMULATED_TOTAL_COUNT_A as accumulatedTotalCountA,");
	sql.append("  A.ACCUMULATED_TOTAL_COUNT_B as accumulatedTotalCountB,");
	sql.append("  A.ACCUMULATED_TOTAL_COUNT_C as accumulatedTotalCountC,");
	sql.append("  A.startCountA as startCountA,");
	sql.append("  A.startCountB as startCountB,");
	sql.append("  A.startCountC as startCountC,");
	sql.append("  B.DEPARTMENT_NAME as departmentName,");
	sql.append("  C.DEVICECATEGORY_NAME as deviceCategoryName, ");
	sql.append("  A.pressure_Grade as pressureGrade ");
	sql.append(" from m_device@DMIS_DBLINK A, m_department@DMIS_DBLINK B, m_devicecategory@DMIS_DBLINK C where A.DEPARTMENT_CID = B.cid and A.DEVICECATEGORY_CID = C.cid and A.DEVICECATEGORY_CID = '2' and A.DEVICE_STATE = '1' ");
	//String conditions = " where s.state = '" + super.getSearchBean().getCon1() + "'"; 
	//String conditions = " where (s.state=0 or s.state=9) "; //有票工作任务单
	String conditions = "";
	 //是否从个人待办事项进入任务单列表 added by wangtao 2012-5-25
	TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
	/* if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "grdbsx".equals(super.getSearchBean().getCon19())){
		 conditions += " AND (s.zbzname = '"+loginUser.getName()+ "'  or  s.zbyname like '%"+loginUser.getName()+ "%')" ;
		 }
	// 工作日期
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
		conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') >= ?";
		valsList.add(super.getSearchBean().getCon2());
	}
	
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
		conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') <= ?";
		valsList.add(super.getSearchBean().getCon3());
	}
	
	// 班组
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
		conditions += " AND s.bgzr = '"+super.getSearchBean().getCon18()+"'";
		
	}
	// 设备类型
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon8())) {
		conditions += " AND b.sblx = '"+super.getSearchBean().getCon8()+"'";
		
	}
	// 设备名称
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
		conditions += " AND b.sbname like '%"+super.getSearchBean().getCon7()+"%'";
		
	}
	// 变电站 by wangtao add20121130
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
		conditions += " AND s.bdz in (";
		StringBuffer buf= new StringBuffer(""); 
		String[] bdznames = ((String) super.getSearchBean().getCon5() == null? "" : super.getSearchBean().getCon5()).split(",");
		
		for (int i=0; i< bdznames.length; i++) { 
		  if (i!=0) buf.append(", "); 
		  buf.append("'" + bdznames[i]+ "'"); 
		 
		} 
		buf.append(")"); 
		conditions +=buf;

		
	}
	//负责人或成员
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon9())){
		 conditions += " AND (s.zbzname = '"+super.getSearchBean().getCon9()+ "'  or  s.zbyname like '%"+super.getSearchBean().getCon9()+ "%')" ;
		 }
	if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
		conditions += " and to_char(s.rq,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
	}
	// 工作计划下的任务单
	if (planid!=null) {
		conditions += " AND s.planid = ?";
		valsList.add(planid);
	}*/
	// 电压等级
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
		conditions += " AND A.pressure_Grade = '"+super.getSearchBean().getCon18()+"'";
		
	}
	// 厂站
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon8())) {
		conditions += " AND A.DEPARTMENT_CID = '"+super.getSearchBean().getCon8()+"'";
		
	}
	// 剩余跳闸次数
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
		if("7".equals(super.getSearchBean().getCon18())||"6".equals(super.getSearchBean().getCon18())||"3".equals(super.getSearchBean().getCon18())){
		conditions += " AND (A.ALLOWABLE_COUNT_A-A.ACCUMULATED_COUNT_A) <= "+super.getSearchBean().getCon7()+"";
		}else{
			conditions += " AND ((A.ALLOWABLE_COUNT_A-A.ACCUMULATED_COUNT_A) <= "+super.getSearchBean().getCon7()+"";
			conditions += " or (A.ALLOWABLE_COUNT_B-A.ACCUMULATED_COUNT_B) <= "+super.getSearchBean().getCon7()+"";
			conditions += " or (A.ALLOWABLE_COUNT_C-A.ACCUMULATED_COUNT_C) <= "+super.getSearchBean().getCon7()+")";
		
		}
		}
	m.put("vals", valsList.toArray());
	m.put("pageflag", "sql");
	m.put("sql", sql.toString() + conditions + "  order by A.DEPARTMENT_CID Desc");
   //班组
 	//String bzsql = "from TOrgan where id <=24 and id >=21 order by id";
 	//bzList = myService.findListByHql(bzsql);
 	//设备类型
 	//DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
	//sblxList = dicService.findList("SBLX");
	//厂站
	String czsql = "select cid as id,department_name as departmentname from m_department@DMIS_DBLINK t where station_grade is not null";
 	sblxList = myService.findListBySql(czsql);
 	//WorkcardDAO workcardDAO=(WorkcardDAO)SpringUtil.getInstance().getBean("workcardDAO");
 	//workcardDAO.closeDbLinkSession("DMIS_DBLINK");//关闭数据库连接
	return m;
	
}
/**
 * 获得左侧树
 */
public String toMonthTree() {
	
	List<HashMap<String, Object>> l = myService.findListBySql("select id as monthid, to_char(czkssj, 'YYYY-MM') as currentmonth from tab_gzrwd where TO_CHAR(czkssj, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");
	if(l.size()!=0){
	currentmonth = l.get(0).get("currentmonth").toString();
	monthid = l.get(0).get("monthid").toString();
	}
	return "TREE_SUCCESS";
}
public void getMonthDate() throws Exception {
	
	List<HashMap<String,Object>> listTree;
	
	// 左侧的树
	if ("-1".equals(month)) {
		
		listTree = myService.findListBySql("select to_char(czkssj,'YYYY') as text, to_char(czkssj,'YYYY') as id, 0 as leaf from tab_gzrwd group by to_char(czkssj,'YYYY') order by id");

	// 维护时根节点
	} else if (month.length() == 4) {
		listTree = myService.findListBySql("select to_char(czkssj,'MM') as text, '" + month + "' || '-' || to_char(czkssj,'MM') as id, 1 as leaf  from tab_gzrwd where to_char(czkssj,'YYYY') = '" + month + "' group by to_char(czkssj,'MM') order by id");
	} else {
		listTree = myService.findListBySql("select to_char(czkssj,'MM') as text, '" + month + "' || '-' || to_char(czkssj,'MM') as id, 1 as leaf  from tab_gzrwd where to_char(czkssj,'YYYY') = '" + month + "' group by to_char(czkssj,'MM') order by id");
	}
	
    JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
	
    JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
    
    super.getResponse().getWriter().print(json.toString());
}
public Page getPageInfo1() {
	return pageInfo1;
}


public List<TGzrwd> getListWp() {
	return listWp;
}

public void setListWp(List<TGzrwd> listWp) {
	this.listWp = listWp;
}

public List<TDictionary> getSblxList() {
	return sblxList;
}

public void setSblxList(List<TDictionary> sblxList) {
	this.sblxList = sblxList;
}

}
